package com.zzk.rxcomponent.ui.home;

import android.os.Bundle;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.zzk.baselibrary.base.RXBaseActivity;
import com.zzk.baselibrary.router.RoutePath;
import com.zzk.rxcomponent.BR;
import com.zzk.rxcomponent.R;
import com.zzk.rxcomponent.databinding.ActivityHomeBinding;

import java.util.ArrayList;
import java.util.List;

import me.majiajie.pagerbottomtabstrip.NavigationController;
import me.majiajie.pagerbottomtabstrip.listener.OnTabItemSelectedListener;

/**
 * @ProjectName: RXMVVMBaseComponent
 * @Package: com.zzk.rxcomponent
 * @ClassName: HomeActivity
 * @Description:
 * @Author: brilliantzhao
 * @CreateDate: 2021.1.14 11:20
 * @UpdateUser:
 * @UpdateDate: 2021.1.14 11:20
 * @UpdateRemark:
 * @Version: 1.0.0
 */
@Route(path = RoutePath.APP.PAGE_HomeActivity)
public class HomeActivity extends RXBaseActivity<HomeViewModel, ActivityHomeBinding> {

    //##########################  custom variables start ##########################################

    private List<Fragment> mFragments;

    //##########################  custom variables end  ###########################################

    //##########################  override custom metohds start ###################################

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_home;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        //初始化Fragment
        initFragment();
        //初始化底部Button
        initBottomTab(mFragments);
    }

    //##########################  override custom metohds end  ####################################

    //##########################  override third methods start ####################################

    //##########################  override third methods end  #####################################

    //##########################  custom metohds start     ########################################

    /**
     *
     */
    private void initFragment() {
        //ARouter拿到多Fragment(这里需要通过ARouter获取，不能直接new,因为在组件独立运行时，宿主app是没有依赖其他组件，所以new不到其他组件的Fragment)
        Fragment mainFragment = (Fragment) ARouter.getInstance().build(RoutePath.MainModule.PAGE_MainFragment).navigation();
        Fragment workFragment = (Fragment) ARouter.getInstance().build(RoutePath.WorkModule.PAGE_WorkFragment).navigation();
        Fragment mineFragment = (Fragment) ARouter.getInstance().build(RoutePath.MineModule.PAGE_MineFragment).navigation();

        mFragments = new ArrayList<>();
        mFragments.add(mainFragment);
        mFragments.add(workFragment);
        mFragments.add(mineFragment);

        if (mainFragment != null) {
            //默认选中第一个
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.add(R.id.frameLayout, mainFragment);
            transaction.commitAllowingStateLoss();
        }
    }

    /**
     *
     */
    private void initBottomTab(List<Fragment> mFragments) {
        NavigationController navigationController = binding.pagerBottomTab.material()
                .addItem(R.mipmap.yingyong, "首页")
                .addItem(R.mipmap.huanzhe, "工作")
                .addItem(R.mipmap.wode_select, "我的")
                .setDefaultColor(ContextCompat.getColor(this, R.color.textColorVice))
                .build();
        //底部按钮的点击事件监听
        navigationController.addTabItemSelectedListener(new OnTabItemSelectedListener() {
            @Override
            public void onSelected(int index, int old) {
                Fragment currentFragment = mFragments.get(index);
                if (currentFragment != null) {
                    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                    transaction.replace(R.id.frameLayout, currentFragment);
                    transaction.commitAllowingStateLoss();
                }
            }

            @Override
            public void onRepeat(int index) {
            }
        });
    }

    //##########################  custom metohds end   ############################################

}
